package com.lanou3g.zjl.chating.home.friends;


import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMValueCallBack;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.home.chatwindow.ChatWindowActivity;
import com.lanou3g.zjl.chating.utils.ProgressLayout;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/21.
 */
public class GroupsActivity extends BaseActivity implements
        ExpandableListView.OnChildClickListener,
        ExpandableListView.OnGroupClickListener,
        PinnedHeaderExpandableListView.OnHeaderUpdateListener, StickyLayout.OnGiveUpTouchEventListener {

    @Bind(R.id.expandablelist)
    PinnedHeaderExpandableListView expandableListView;

    @Bind(R.id.sticky_layout)
    StickyLayout stickyLayout;
    @Bind(R.id.sticky_content)
    ProgressLayout sticky_content;
    //适配器
    private MyexpandableListAdapter adapter;
    //群列表
    List<EMGroup> grouplist;

    @Override
    protected int getLayout() {
        return R.layout.groups_activity;
    }

    @Override
    protected void initData() {


    }
    //返回按钮
    @OnClick(R.id.backToFriendsList) void  backToFriendsList(){
        finish();
    }
    @Override
    protected void onResume() {
        super.onResume();
        sticky_content.showProgress();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //获取群列表
                    grouplist = EMGroupManager.getInstance().getGroupsFromServer();

                } catch (EaseMobException e) {
                    Log.d("GroupsActivity", "网络不好，叫蓝鸥网管");
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new MyexpandableListAdapter(GroupsActivity.this, grouplist);
                        expandableListView.setAdapter(adapter);
                        initView();
                        sticky_content.showContent();
                    }
                });
            }
        }).start();
    }

    //初始化适配器以及各种点击事件
    private void initView() {
        // 展开所有group
        for (int i = 0, count = expandableListView.getCount(); i < count; i++) {
            expandableListView.expandGroup(i);
        }
        expandableListView.setOnHeaderUpdateListener(this);
        expandableListView.setOnChildClickListener(this);
        expandableListView.setOnGroupClickListener(this);
        stickyLayout.setOnGiveUpTouchEventListener(this);
    }

    //listview的各种点击事件
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Intent intent = new Intent(GroupsActivity.this, ChatWindowActivity.class);
        intent.putExtra("groupId",adapter.getChild(groupPosition, childPosition).getGroupId());
        intent.putExtra("chattype", 2);
        intent.putExtra("groupName",adapter.getChild(groupPosition, childPosition).getGroupName());
        startActivity(intent);
        return false;
    }

    @Override
    public boolean giveUpTouchEvent(MotionEvent event) {
        if (expandableListView.getFirstVisiblePosition() == 0) {
            View view = expandableListView.getChildAt(0);
            if (view != null && view.getTop() >= 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
        return false;
    }

    @Override
    public View getPinnedHeader() {
        View headerView = getLayoutInflater().inflate(R.layout.grouplist_header, null);
        headerView.setLayoutParams(new AbsListView.LayoutParams(
                AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        return headerView;
    }

    @Override
    public void updatePinnedHeader(View headerView, int firstVisibleGroupPos) {
        String firstVisibleGroup = adapter.getGroup(firstVisibleGroupPos);
        TextView textView = (TextView) headerView.findViewById(R.id.groupHeaderText);
        textView.setText(firstVisibleGroup);
    }

    //点击跳转到新建群组页面
    @OnClick(R.id.createNewGroup)
    void gotoCreateGroup() {
        Intent intent = new Intent(GroupsActivity.this, CreateGroupChatActivity.class);
        startActivity(intent);
    }

    //点击跳转到加入公开群页面
    @OnClick(R.id.joinPublicGroup)
    void joinPublicGroup() {
        Intent intent = new Intent(GroupsActivity.this, PublicGroupActivity.class);
        startActivity(intent);
    }
}
